/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package login;

import databaseManager.DbConn;
import javax.servlet.http.HttpServletRequest;
import java.sql.*;
import java.util.Calendar;


/**
 *
 * @author 47
 */
public class Login {

    public Login() {
    }

    private HttpServletRequest httpRequest;

    private String userName = null;
    private String userPassword = null;
    String message="Hello";
    public String getMassage(){
        return this.message;
    }

    public String getTime(){
        String str=null;
        java.util.Calendar cal=Calendar.getInstance();
        str=String.valueOf(cal.get(cal.YEAR))+"年"
                +String.valueOf(cal.get(cal.MONTH)+1)+"月"
                +String.valueOf(cal.get(cal.DATE))+"日";
        return str;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the userPassword
     */
    public String getUserPassword() {
        return userPassword;
    }

    /**
     * @param userPassword the userPassword to set
     */
    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public boolean getLoginResult() {
        boolean isOK=false;
        if(this.userName!=null){
            if(!this.userName.equals("")){
                Connection conn=null;
                Statement stmt=null;
                ResultSet rs=null;
                try{
                    conn=DbConn.getNewDbconn();
                    stmt=conn.createStatement();
                    rs=stmt.executeQuery("select user_password from usertable where user_name='"+this.userName+"'");
                    if(rs.next()&&this.userPassword!=null){
                        String password=rs.getString("user_password");
                        if(password.equals(this.userPassword)){
                            this.httpRequest.getSession().setAttribute("username", this.userName);
                            isOK=true;
                        }else{
                            isOK=false;
                        }
                    }else{
                        isOK=false;
                    }
                }catch(Exception ex){
                    isOK=false;
                    this.message=ex.getMessage();
                }finally{
                    if(rs!=null){
                        try{
                    rs.close();
                        }catch(Exception rse){

                        }
                    }
                    if(stmt!=null){
                        try{
                            stmt.close();
                        }catch(Exception stmte){

                        }
                    }
                    if(conn!=null){
                        try{
                            conn.close();
                        }catch(Exception conne){
                            
                        }
                    }
                }
            }
        }
        return isOK;
    }

    /**
     * @return the httpRequest
     */
    public HttpServletRequest getHttpRequest() {
        return httpRequest;
    }

    /**
     * @param httpRequest the httpRequest to set
     */
    public void setHttpRequest(HttpServletRequest httpRequest) {
        this.httpRequest = httpRequest;
    }
}
